﻿using EV.EventBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.ViewModels;

namespace ZoneLine.Common
{
    public class Speaker
    {
        private static Lazy<Speaker> _obj = new Lazy<Speaker>(() => new Speaker(), System.Threading.LazyThreadSafetyMode.PublicationOnly);

        private string _eventId = Guid.NewGuid().ToString();

        private SpeechSynthesizer synthesizer;

        private Speaker()
        {
            MessageCenter.Subscribe(nameof(Speaker), _eventId, (ss) => { SpeakInfo(ss.Source.Payload?.ToString()); return Task.CompletedTask; });
            synthesizer = new SpeechSynthesizer();
        }

        public static Speaker GetSpeaker()
        {
            return _obj.Value;
        }

        private void SpeakInfo(string info)
        {
            if (!string.IsNullOrEmpty(info))
            {
                //synthesizer.Pause();
                {
                    // 设置语音属性，例如音量和语速
                    synthesizer.Volume = 55; // 0到100
                    synthesizer.Rate = 0;     // -10到10

                    // 设置语音合成语言为中文（简体）
                    synthesizer.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult, 0, new System.Globalization.CultureInfo("zh-CN"));

                    // 开始朗读文本
                    var rlt = synthesizer.SpeakAsync(info);
                }
            }
        }
    }
}